package com.h3c.tj_nsh.Utils;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.View;

import com.h3c.tj_nsh.Constant;
import com.h3c.tj_nsh.MyApplication;
import com.h3c.tj_nsh.UI.Activities.DepartmentLeaderActivity;
import com.h3c.tj_nsh.UI.Activities.EngineerActivity;
import com.h3c.tj_nsh.UI.Activities.RequestServiceActivity;
import com.h3c.tj_nsh.UI.Activities.TopLeaderActivity;


/**
 * Created by cfw2445 on 2016/4/22.
 */
public class UIUtils {
    private static MyApplication sMyApplication;
    // 获取context对象
    public static Context getContext() {
        return MyApplication.getContext();
    }

    // 获取主线程的handler
    public static Handler getMainThreadHandler() {
        return MyApplication.getHandler();
    }

    // 获取主线程的线程id
    public static int getMainThreadId() {
        return MyApplication.getMainThreadId();
    }

    // 加载布局文件
    public static View inflateView(int id) {
        return View.inflate(getContext(), id, null);
    }

    // 判断是否在主线程中运行
    public static boolean isRunOnUiThread() {
        int currentThreadId = android.os.Process.myTid();// 获取当前线程所在的id
        return currentThreadId == getMainThreadId();
    }

    // 让r一定在主线程当中执行
    public static void runOnUiThread(Runnable r) {
        if (isRunOnUiThread()) {
            r.run();// r.start--启动新的线程
        } else {
            getMainThreadHandler().post(r);// 把r放到主线程的消息队列执行
        }
    }

    /**
     * 打开主页面
     *
     * @param context
     */
    public static void showMain(Context context, int level) {
        sMyApplication = (MyApplication) context.getApplicationContext();
        sMyApplication.setUserLevel(level);
        Intent intent = null;

        switch (level) {
            case Constant.TOP_LEADER:
                intent = new Intent(getContext(), TopLeaderActivity.class);
                break;
            case Constant.DEPARTMENT:
                intent = new Intent(getContext(), DepartmentLeaderActivity.class);
                break;
            case Constant.ENGINEER:
                intent = new Intent(getContext(), EngineerActivity.class);
                break;
        }
        context.startActivity(intent);
    }

    public static void showITSMServiceRequest(FragmentActivity activity) {
        Intent intent=new Intent(activity, RequestServiceActivity.class);
        activity.startActivity(intent);

    }
}
